﻿
Imports Windows.Storage
Imports Windows.Storage.FileProperties

Public Module _list
    Private Class meta
        Private ReadOnly f As StorageFile = Nothing
        Private d As String = Nothing

        Public Sub New(ByVal f As StorageFile)
            assert(Not f Is Nothing)
            Me.f = f
            Me.d = f.Name()
            update().Start()
        End Sub

        Private Async Function update() As Task
            Dim p As MusicProperties = Nothing
            p = Await f.Properties.GetMusicPropertiesAsync()
            d = String.Concat(p.Title(),
                              combine(p.Subtitle()),
                              combine(p.Album()),
                              combine(p.Artist()),
                              combine(New DateTime(p.Duration().Ticks())),
                              combine(p.Rating()))
        End Function

        Private Shared Function combine(Of T)(ByVal i As T) As String
            Dim s As String = Nothing
            s = Convert.ToString(i)
            If String.IsNullOrWhiteSpace(s) Then
                Return Nothing
            Else
                Return String.Concat(" - ", s)
            End If
        End Function
    End Class

    Private ReadOnly l As IList(Of meta) = Nothing

    Private Sub unlock_push_back(ByVal i As StorageFile)
        If Not i Is Nothing Then
            l.Add(New meta(i))
        End If
    End Sub

    Public Sub push_back(ByVal i As IReadOnlyList(Of StorageFile))
        If Not i Is Nothing AndAlso i.Count() > 0 Then
            SyncLock l
                For Each j In i
                    unlock_push_back(j)
                Next
            End SyncLock
        End If
    End Sub

    Public Sub push_back(ByVal i As StorageFile)
        SyncLock l
            unlock_push_back(i)
        End SyncLock
    End Sub

    Sub New()
        l = New List(Of meta)()
    End Sub
End Module
